﻿Imports Utilidades
Imports Excepciones
Imports Negocio
Imports Servicios
Imports System.IO

Public Class frmABMUsuario

    Private _usuario As New Usuario()

    Protected Overrides Sub PreLoad()
        Me.Icon = Icon.FromHandle(Global.SIPRO.My.Resources.Resources.People.GetHicon())
    End Sub

    Sub New()
        InitializeComponent()
    End Sub
    Sub New(usuario As Usuario)
        Me.New()
        _usuario = usuario
        btnGuardar.Tag = "Modificar"
        txtNick.ReadOnly = True
        cbRecursos.Enabled = False
    End Sub

    Private Sub frmNuevoIdioma_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Init()
        txtNick.DataBindings.Add("Text", _usuario, "Nick")
        txtClave.DataBindings.Add("Text", _usuario, "Clave")
        cbPerfiles.DataBindings.Add("SelectedItem", _usuario, "Perfil")
        cbIdiomas.DataBindings.Add("SelectedItem", _usuario, "Idioma")
        cbRecursos.DataBindings.Add("SelectedItem", _usuario, "Recurso")
    End Sub

    Private Sub Init()

        Try
            Dim idis As List(Of Idioma) = GesIdiomas.Consultar(Nothing)
            cbIdiomas.DataSource = idis
        Catch ex As Exception
            My.Application.HandlerException(ex)
            Me.Close()
        End Try

        Try
            Dim perf As List(Of Perfil) = GesPerfiles.Consultar(Nothing)
            cbPerfiles.DataSource = perf
        Catch ex As Exception
            My.Application.HandlerException(ex)
            Me.Close()
        End Try

        Try
            Dim rec As List(Of Recurso) = GesRecursos.Consultar(Nothing, Nothing, Nothing)
            cbRecursos.DataSource = rec
        Catch ex As Exception
            My.Application.HandlerException(ex)
            Me.Close()
        End Try

    End Sub

    Private Sub OK_Click(sender As System.Object, e As System.EventArgs) Handles btnGuardar.Click
        If MyBase.ValidateForm() Then
            Try
                _usuario.OID = _usuario.Recurso.OID
                GesUsuarios.Guardar(_usuario)
                Me.Close()
                Me.DialogResult = Windows.Forms.DialogResult.OK
            Catch ex As Exception
                My.Application.HandlerException(New UsuarioSaveException())
            End Try
        End If

    End Sub

    Private Sub Cancel_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()
    End Sub

End Class